package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.app.miniapp.models.WxArea;
import com.budwk.app.miniapp.services.WxareaService;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@IocBean
@At("/platform/miniapp/area")
@ApiDefinition(tag = "微信小程序-行政区域")
@Slf4j
public class WxareaController {
    @Inject
    private WxareaService wxAreaService;

    /**
     * 查询行政区域列表
     * @param bean
     * @param pageNo
     * @param pageSize
     * @param pageOrderName
     * @param pageOrderBy
     * @return
     */
    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询行政区域")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxArea.class
    )
    @ApiResponses(
            implementation = WxArea.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxArea bean,
                          @Param("pageNo") int pageNo,
                          @Param("pageSize") int pageSize,
                          @Param("pageOrderName") String pageOrderName,
                          @Param("pageOrderBy") String pageOrderBy) {
        return Result.data(wxAreaService.listPage(pageNo, pageSize, wxAreaService.querySqlBuild(bean, pageOrderName, pageOrderBy)));
    }

    /**
     * 导出行政区划列表
     * @param bo
     * @param response
     */
    @At("/export")
    @Ok("json")
    @POST
    @ApiOperation(name = "导出行政区划")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxArea.class
    )
    @ApiResponses
    @SaCheckLogin
    public void export(WxArea bo, HttpServletResponse response) {
        Cnd cnd = Cnd.NEW();
        //cnd.and(new Static(wxAreaService.querySqlBuild(bo, null, null)));

        List<WxArea> list = wxAreaService.query(cnd);
        //ExcelUtil.exportExcel(list, "行政区划", WxArea.class, response);
    }

    /**
     * 新增行政区域
     * @param wxArea
     * @param req
     * @return
     */
    @At("/add")
    @Ok("json")
    @POST
    @ApiOperation(name = "新增行政区域")
    @ApiFormParams(
            implementation = WxArea.class
    )
    @ApiResponses
    @SLog(tag = "新增行政区域", value = "${args[0].id}")
    @SaCheckLogin
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> create(@Param("..") WxArea wxArea, HttpServletRequest req) {
        //wxArea.setCreatedBy(SecurityUtil.getUserId());
        wxAreaService.insert(wxArea);
        return Result.success();
    }

    /**
     * 修改行政区域
     * @param wxArea
     * @param req
     * @return
     */
    @At("/upadte")
    @Ok("json")
    @POST
    @ApiOperation(name = "修改行政区域")
    @ApiFormParams(
            implementation = WxArea.class
    )
    @ApiResponses
    @SLog(tag = "修改行政区域", value = "${args[0].id}")
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> update(@Param("..") WxArea wxArea, HttpServletRequest req) {
        //wxArea.setUpdatedBy(SecurityUtil.getUserId());
        wxAreaService.updateIgnoreNull(wxArea);
        return Result.success();
    }

    /**
     * 获取行政区划详细信息
     * @param id
     * @param req
     * @return
     */
    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取行政区域")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @ApiResponses
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxArea wxArea = wxAreaService.fetch(id);
        if (wxArea == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxArea);
    }

    /**
     * 删除行政区域
     * @param id
     * @param req
     * @return
     */
    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除行政区域")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除行政区域", value = "${args[0]}")
    @SaCheckLogin
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxArea wxArea = wxAreaService.fetch(id);
        if (wxArea == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxAreaService.delete(id);
        req.setAttribute("_slog_msg", wxArea.getId());
        return Result.success();
    }
}
